package com.gzhryc.shared_device.oem.code.config.services;

import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.oem.code.BaseConstants;
import com.gzhryc.shared_device.oem.code.config.dao.LogisticsCompanyDao;
import com.gzhryc.shared_device.oem.code.config.dao.db.LogisticsCompany;
import com.gzhryc.shared_device.oem.code.config.dao.enums.LogisticsCompanyEnum;
import com.gzhryc.shared_device.oem.code.config.services.dto.LogisticsCompanySearch;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class LogisticsCompanyService {

    static Logger log = Logger.getLogger(LogisticsCompanyService.class);

    public static LogisticsCompanyService self() {
        return MultiDBTools.getService(BaseConstants.JDBC_KEY,LogisticsCompanyService.class, true);
    }

    public static LogisticsCompanyService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, LogisticsCompanyService.class, true);
    }

    LogisticsCompanyDao dao;

    public LogisticsCompanyService(String jdbcKey) {
        this.dao = new LogisticsCompanyDao(jdbcKey);
    }

    public List<LogisticsCompany> findBySearch(LogisticsCompanySearch search, Integer pageNo, Integer pageLimit){
        Conditions conditions = dao.buildConditions(search);
        try {
            return dao.findByCondition(conditions,pageNo,pageLimit);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public Long countBySearch(LogisticsCompanySearch search){
        Conditions conditions = dao.buildConditions(search);
        try {
            return dao.count(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }

    public LogisticsCompany getById(String id){
        try {
            return dao.getById(id);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public LogisticsCompany getDefLogisticsCompany(){
        Conditions conditions = new Conditions(LogisticsCompany.class);
        conditions.and().is("state", LogisticsCompanyEnum.State.Default.index());
        try {
            return  dao.get(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public boolean add(LogisticsCompany logisticsCompany){
        logisticsCompany.setCreateDate(new Date());
        try {
            return dao.insert(logisticsCompany) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean updateNotNull(LogisticsCompany logisticsCompany){
        try {
            return dao.updateNotNull(logisticsCompany) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean deleteById(String id){
        try {
            return dao.deleteById(id) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }
}
